<template>
  <header class="navbar">
    <!-- 左侧：品牌Logo和返回按钮 -->
    <div class="navbar-left">
      <button 
        v-if="showBack" 
        class="back-btn"
        @click="handleBack"
      >
        <i class="fas fa-arrow-left"></i>
      </button>
      <router-link to="/" class="logo">
        <i class="fas fa-feather"></i>
        <span class="logo-text">社交平台</span>
      </router-link>
    </div>
    
    <!-- 中间：搜索框 -->
    <div class="navbar-center">
      <div class="search-box">
        <i class="fas fa-search search-icon"></i>
        <input 
          v-model="searchQuery" 
          type="text" 
          placeholder="搜索用户、帖子..."
          @keyup.enter="handleSearch"
        >
      </div>
    </div>
    
    <!-- 右侧：用户菜单和功能按钮 -->
    <div class="navbar-right">
      <div class="nav-buttons">
        <router-link to="/explore" class="nav-btn">
          <i class="fas fa-compass"></i>
          <span>发现</span>
        </router-link>
        <router-link to="/notifications" class="nav-btn">
          <i class="fas fa-bell"></i>
          <NotificationBadge />
          <span>通知</span>
        </router-link>
        <router-link to="/messages" class="nav-btn">
          <i class="fas fa-comment"></i>
          <span>消息</span>
        </router-link>
      </div>
      
      <div class="user-menu" @click="toggleUserMenu">
        <div class="user-avatar">
          <img :src="userAvatar" alt="用户头像">
        </div>
        <div class="menu-dropdown" v-show="showUserMenu">
          <router-link to="/profile" class="menu-item">
            <i class="fas fa-user"></i>
            <span>个人主页</span>
          </router-link>
          <router-link to="/settings" class="menu-item">
            <i class="fas fa-cog"></i>
            <span>设置</span>
          </router-link>
          <div class="menu-item" @click="handleLogout">
            <i class="fas fa-sign-out-alt"></i>
            <span>退出登录</span>
          </div>
        </div>
      </div>
    </div>
  </header>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { useAuthStore } from '@/store/modules/auth';
import NotificationBadge from '../notification/NotificationBadge.vue';

const router = useRouter();
const route = useRoute();
const authStore = useAuthStore();

// 状态
const searchQuery = ref('');
const showUserMenu = ref(false);
const showBack = ref(false);

// 用户信息（从 store 获取）
const userAvatar = computed(() => authStore.currentUser?.avatar || 'https://via.placeholder.com/40');

// 监听路由变化，更新返回按钮状态
watch(
  () => route.path,
  (newPath) => {
    showBack.value = newPath !== '/';
  }
);

// 处理返回按钮点击
const handleBack = () => {
  router.back();
};

// 处理搜索
const handleSearch = () => {
  if (searchQuery.value.trim()) {
    router.push(`/search?query=${searchQuery.value}`);
  }
};

// 切换用户菜单
const toggleUserMenu = (e) => {
  e.stopPropagation();
  showUserMenu.value = !showUserMenu.value;
};

// 点击页面其他地方关闭用户菜单
onMounted(() => {
  document.addEventListener('click', () => {
    showUserMenu.value = false;
  });
});

// 处理退出登录
const handleLogout = () => {
  authStore.logout();
  router.push('/login');
};
</script>

<style scoped>
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 60px;
  background-color: #fff;
  border-bottom: 1px solid #eee;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.navbar-left, .navbar-center, .navbar-right {
  display: flex;
  align-items: center;
}

.navbar-left {
  flex: 0 0 auto;
}

.back-btn {
  background: none;
  border: none;
  color: #666;
  font-size: 18px;
  margin-right: 15px;
  cursor: pointer;
}

.logo {
  display: flex;
  align-items: center;
  color: #007bff;
  text-decoration: none;
  font-size: 20px;
  font-weight: bold;
}

.logo i {
  margin-right: 8px;
}

.navbar-center {
  flex: 1;
  max-width: 400px;
  margin: 0 20px;
}

.search-box {
  position: relative;
  width: 100%;
}

.search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: #999;
}

.search-box input {
  width: 100%;
  padding: 8px 10px 8px 30px;
  border: 1px solid #ddd;
  border-radius: 16px;
  font-size: 14px;
  outline: none;
  background-color: #f5f5f5;
}

.navbar-right {
  flex: 0 0 auto;
}

.nav-buttons {
  display: flex;
  margin-right: 20px;
}

.nav-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  margin: 0 10px;
  text-decoration: none;
  color: #666;
  position: relative;
}

.nav-btn i {
  margin-bottom: 4px;
}

.nav-btn span {
  font-size: 10px;
  display: none; /* 移动端隐藏文字，PC端可显示 */
}

.user-menu {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.user-avatar img {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
}

.menu-dropdown {
  position: absolute;
  right: 20px;
  top: 60px;
  background-color: #fff;
  border: 1px solid #eee;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  padding: 10px 0;
  z-index: 101;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 8px 20px;
  text-decoration: none;
  color: #333;
  font-size: 14px;
}

.menu-item i {
  margin-right: 10px;
  width: 20px;
}

.menu-item:hover {
  background-color: #f5f5f5;
}
</style>